package cn.aitrox.ry.service.dwh.remote;

import cn.aitrox.ry.common.exception.CustomBizException;
import cn.aitrox.ry.service.dwh.config.DwhNacosProperties;
import cn.aitrox.ry.service.dwh.dto.in.jy.ImageQueryInDto;
import cn.aitrox.ry.service.dwh.dto.in.jy.SeriesQueryInDto;
import cn.aitrox.ry.service.dwh.dto.out.jy.ApiOutputDto;
import cn.aitrox.ry.service.dwh.dto.out.jy.ImageItemDto;
import cn.aitrox.ry.service.dwh.dto.out.jy.ImageQueryOutDto;
import cn.aitrox.ry.service.dwh.dto.out.jy.SeriesItemDto;
import cn.aitrox.ry.service.dwh.dto.out.jy.SeriesQueryOutDto;
import cn.aitrox.ry.util.ArrayUtil;
import cn.aitrox.ry.util.JSONUtil;
import cn.aitrox.ry.util.LogUtil;
import cn.aitrox.ry.util.NetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class JyDwhApi {

    private static final Logger LOG = LoggerFactory.getLogger(JyDwhApi.class);

    private static final String DWH_LOGIN_FREE_KEY = "loginFreeKey";

    @Autowired
    private DwhNacosProperties nacosProp;

    public List<ImageItemDto> imageQuery(String seriesUid, String fileType) {
        try {
            ImageQueryInDto in = new ImageQueryInDto(seriesUid, fileType);
            ApiOutputDto apiOutput = this.httpPostString(nacosProp.getJyDwhImageQueryUrl(), JSONUtil.toJson(in, ImageQueryInDto.class), false, "ImageQuery", ImageQueryOutDto.class);
            if (!apiOutput.isSuccess()) {
                throw new CustomBizException(apiOutput.getMsg());
            }
            ImageQueryOutDto out = (ImageQueryOutDto) apiOutput.getData();
            return out.getList();

        } catch (CustomBizException e1) {
            throw e1;
        } catch (Exception e2) {
            LOG.error(LogUtil.genException(e2, "JyDwhApi.imageQuery fail"), e2);
            throw new CustomBizException("调用数仓查询序列影像失败");
        }
    }

    public List<SeriesItemDto> seriesQuery(List<String> seriesUids) {
        if (ArrayUtil.isEmpty(seriesUids)) {
            return new ArrayList<>();
        }
        try {
            SeriesQueryInDto in = new SeriesQueryInDto(seriesUids);
            ApiOutputDto apiOutput = this.httpPostString(nacosProp.getJyDwhSeriesQueryUrl(), JSONUtil.toJson(in, SeriesQueryInDto.class), false, "SeriesQuery", SeriesQueryOutDto.class);
            if (!apiOutput.isSuccess()) {
                throw new CustomBizException(apiOutput.getMsg());
            }
            SeriesQueryOutDto out = (SeriesQueryOutDto) apiOutput.getData();
            return out.getList();
        } catch (CustomBizException e1) {
            throw e1;
        } catch (Exception e2) {
            LOG.error(LogUtil.genException(e2, "JyDwhApi.seriesQuery fail"), e2);
            throw new CustomBizException("调用数仓查询序列详情失败");
        }
    }

    private ApiOutputDto httpPostString(String url, String jsonParam, boolean isArrayResponse, String method, Class clazz) {
        Map<String, String> headers = new HashMap<>();
        headers.put(DWH_LOGIN_FREE_KEY, nacosProp.getJyDwhLoginFreeKey());
        String responseStr = NetUtil.httpPostString(url, jsonParam, headers, "application/json", method);
        ApiOutputDto apiOutput = JSONUtil.fromJson(responseStr, ApiOutputDto.class);

        if (apiOutput.isSuccess()) {
            Object data = apiOutput.getData();
            if (null != data && !(data instanceof String)) {
                if (isArrayResponse) {
                    apiOutput.setData(JSONUtil.parseArray(JSONUtil.toJson(data), clazz));
                } else {
                    apiOutput.setData(JSONUtil.fromJson(JSONUtil.toJson(data), clazz));
                }
            }
        }
        return apiOutput;
    }

}
